var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
$(function(){
	var usercode = $("#user-name",parent.parent.document).attr("code");
	var userkey = $("#user-name",parent.parent.document).attr("key");
	var usertype = $("#user-name",parent.parent.document).attr("usertype");
	keyCheck(usercode,usertype,userkey,function(){
		initSmPages();
		window.onresize = function () {
			initSmPages();
		};
		pageBtns();
		getAllRings();
	});
});
function getAllRings(){
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var imei = $(".students-choice-select",parent.document).attr("imei");
	var data = {
			cmd:"getAllRings",
			model:"card",
			userKey:userKey,
			data:{
				
			},
			remark:"",
	};
	AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var list = res.data;
			var content = "";
			for(var i = 0; i < list.length; i = i+2){
				content += "<tr><td><input type=\"radio\" name=\"terminalsetup_name\" value='"+list[i].ring+"' ringurl='"+list[i].ringurl+"'/><a href='"+Host+list[i].ringurl+"' target=\"view_window\" class=\"rings\">铃声"+Number(list[i].ring)+"</a></td>";
				content += "<td><input type=\"radio\" name=\"terminalsetup_name\" value='"+list[i+1].ring+"' ringurl='"+list[i].ringurl+"' /><a href='"+Host+list[i+1].ringurl+"' target=\"view_window\" class=\"rings\">铃声"+Number(list[i+1].ring)+"</a></td></tr>";
			}
			$("#table-ring").empty();
			$("#table-ring").append(content);
			getSelectedRing(imei);
			
		}
	},null,null);	
}
function getSelectedRing(imei){
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var data = {
			cmd:"getCard",
			model:"card",
			userKey:userKey,
			data:{
				imei:imei,
			},
			remark:"",
	};
	AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var list = res.data;
			//显示已有的铃声设置
			var m = $("#table-ring input");
			for(var i = 0; i < m.length; i++){
				if(list[0].ring != "" && list[0].ring != null && list[0].ring != undefined){
					if(m[i].getAttribute("value") == list[0].ring){}
					$("#table-ring input[value="+list[0].ring+"]").attr("checked","checked");
				}else{
					$("#table-ring input[value='1']").attr("checked","checked");
				}
			}
		}
	},null,null);	
}
function addSelectedRing(){
	var imei = $(".students-choice-select",parent.document).attr("imei");
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var ring = $("#table-ring input[name=\"terminalsetup_name\"]:checked").val();
	var ringurl = $("#table-ring input[name=\"terminalsetup_name\"]:checked").attr("ringurl");
	var data = {
			cmd:"updateCard",
			model:"card",
			userKey:userKey,
			data:{
				imei:imei,
				ring:ring,
				ringurl:ringurl,
			},
			remark:"",
	};
	processBegin("正在应用中···");
	AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			processEnd("设置成功");
			getAllRings();
		}else{
			processError("设置失败");
		}
	},null,null);	
}
function pageBtns(){
	$(".students-choice",parent.document).unbind("click");
	$(".students-choice",parent.document).click(function(){
		$(".students-choice",parent.document).removeClass("students-choice-select");
		$(this).addClass("students-choice-select");
		getAllRings();
	});
	$("#terminalsetup-add").click(function(){
		addSelectedRing();
	});
}
function initSmPages(){
	var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
	$(".right-content").height(rcH);
	
	var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
	$(".content-data-show").height(cdsH);
}